package com.atguigu.gmall.order.config;

import com.atguigu.gmall.common.constant.MqConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 李旭
 * @create: 2021-09-14 09:27
 *
 *  延迟消息
 *     基于插件
 *     基于死信的
 *     Configuration: 配置  Java配置类
 *     Component ： 实例化的注解
 *       语义上区别
 **/
//@Configuration //@Component
@Component
public class OrderInfoConfig {


    //交换机
    @Bean
    public Exchange orderCancelExchange(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(MqConst.EXCHANGE_DIRECT_ORDER_CANCEL,
                "x-delayed-message",true,false,arguments);
    }
    //取消订单队列
    @Bean
    public Queue orderCancelQueue(){
        return QueueBuilder.durable(MqConst.QUEUE_ORDER_CANCEL).build();
    }
    //关闭交易的队列
    @Bean
    public Queue closeTradeQueue(){
        return QueueBuilder.durable(MqConst.QUEUE_PAYMENT_CLOSE).build();
    }
    @Bean
    public Binding bindingOrderCancelQueueToOrderCancelExchange(
            @Qualifier(value = "orderCancelQueue") Queue orderCancelQueue,
            @Qualifier(value = "orderCancelExchange") Exchange orderCancelExchange
    ){
        return BindingBuilder.bind(orderCancelQueue).to(orderCancelExchange)
                .with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }
    @Bean
    public Binding bindingCloseTradeQueueToCloseTradeExchange(
            @Qualifier(value = "closeTradeQueue") Queue closeTradeQueue,
            @Qualifier(value = "orderCancelExchange") Exchange orderCancelExchange
    ){
        return BindingBuilder.bind(closeTradeQueue).to(orderCancelExchange)
                .with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }
}
